from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
import os


class SelectOperation:
    def __init__(self):
        # 打开浏览器
        self.driver = webdriver.Chrome()
        # 获取动态地址
        self.url = 'file:///' + os.path.abspath('html') + '/注册实例.html'
        self.driver.get(self.url)
        # 最大化
        self.driver.maximize_window()

    def frist_select(self):
        # 创建select对象
        s1 = self.driver.find_element(By.ID, 'select')
        select = Select(s1)
        time.sleep(2)
        # 根据值选中下拉框里的按钮
        select.select_by_value('sh')

    def second_select(self):
        # 切换frame页面
        self.driver.switch_to.frame('idframe1')
        # 创建select对象
        s2 = self.driver.find_element(By.ID, 'selectA')
        select = Select(s2)
        time.sleep(2)
        # 根据索引选中下拉框里的按钮
        select.select_by_index(3)

    def third_select(self):
        # 返回到最上层的frame页面
        self.driver.switch_to.default_content()
        # 切换frame页面
        f3 = self.driver.find_element(By.XPATH, '/html/body/s/iframe[2]')
        self.driver.switch_to.frame(f3)
        # 创建select对象
        s3 = self.driver.find_element(By.ID, 'selectB')
        select = Select(s3)
        time.sleep(2)
        # 根据文本链接选中下拉框里的按钮
        select.select_by_visible_text('广州B')

    def quit(self):
        time.sleep(2)
        # 关闭浏览器
        self.driver.quit()


if __name__ == '__main__':
    s1 = SelectOperation()
    s1.frist_select()
    s1.second_select()
    s1.third_select()
    s1.quit()
